package com.xqkad.myplayer.service;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.xqkad.myplayer.activity.LockActivity;

import java.lang.reflect.Field;
import java.util.List;

import static com.xqkad.myplayer.activity.LogActivity.log;

public class BroadcastService extends LogService{
    private Receiver receiver;
    private static final String HEAD    = "xqkad.action.media.";

    public interface Actions{
        String ON_PLAY = HEAD+"ON_PLAY";
        String ON_PAUSE = HEAD+"ON_PAUSE";
        String ON_STOP = HEAD+"ON_STOP";
        String ON_PREVIOUS = HEAD+"ON_PREVIOUS";
        String ON_NEXT = HEAD+"ON_NEXT";
        String ON_PLAYMODE_CHANGE = HEAD + "playmode_change";

        String TO_TOGGLE_PLAY_PAUSE = HEAD+"TO_TOGGLE_PLAY_PAUSE";
        String TO_PLAY = HEAD+"TO_PLAY";
        String TO_PAUSE = HEAD+"TO_PAUSE";
        String TO_STOP = HEAD+"TO_STOP";
        String TO_PREVIOUS = HEAD+"TO_PREVIOUS";
        String TO_NEXT = HEAD+"TO_NEXT";
    }
    public static IntentFilter getIntentFilter(){
        IntentFilter it = new IntentFilter();
        Field[] fields = Actions.class.getFields();
        for (int i=0;i<fields.length;i++){
            try {
                String val = (String) fields[i].get(null);
                it.addAction(val);
            } catch (Exception e) {
                Log.d(TAG, "catch: fields[i].get(null)");
            }
        }
        return it;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        receiver = new Receiver();
        IntentFilter filter = getIntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(receiver,new IntentFilter(filter));
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    protected boolean isPlaying(){return false;}
    protected void togglePlayPause(){if(isPlaying()) pause();else play();}
    protected void play(){
        sendBroadcast(new Intent(Actions.ON_PLAY));
    }
    protected void pause(){
        sendBroadcast(new Intent(Actions.ON_PAUSE));
    }
    protected void stop(){
        sendBroadcast(new Intent(Actions.ON_STOP));
    }
    protected void previous(){
        sendBroadcast(new Intent(Actions.ON_PREVIOUS));
    }
    protected void next(){
        sendBroadcast(new Intent(Actions.ON_NEXT));
    }
    protected void setPlayMode(){sendBroadcast(new Intent(Actions.ON_PLAYMODE_CHANGE));}

    protected void onSreenOff(){
        if(isPlaying() && !checkapptask()){
            Intent intent = new Intent(this, LockActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
    private boolean checkapptask(){
        ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.AppTask> list = am.getAppTasks();
        ActivityManager.RecentTaskInfo task;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            for(int i=0;i<list.size();i++){
                task = list.get(i).getTaskInfo();
                if(task.baseActivity != null){
                    if(task.baseActivity.getClassName().equals(LockActivity.class.getName())){
                        return true;
                    }
                }
            }
        }else{
            List<ActivityManager.RunningTaskInfo> runlist = am.getRunningTasks(11);
            for(int i = 0; i<runlist.size();i++){
                ActivityManager.RunningTaskInfo item = runlist.get(i);
                if(item != null && item.topActivity != null){
                    String top = item.topActivity.getClassName();
                    if(top.equals(LockActivity.class.getName())){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    protected void onScreenOn(){}

    private class Receiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            //Log.d(TAG, "onBSR: "+action);

            switch (action != null ? action : "") {
                case Actions.TO_TOGGLE_PLAY_PAUSE:
                    togglePlayPause();
                    break;
                case Actions.TO_PLAY:
                    play();
                    break;
                case Actions.TO_PAUSE:
                    pause();
                    break;
                case Actions.TO_PREVIOUS:
                    previous();
                    break;
                case Actions.TO_NEXT:
                    next();
                    break;
                case Actions.TO_STOP:
                    stop();
                    break;
                case Intent.ACTION_SCREEN_OFF:
                    onSreenOff();
                    break;
                case Intent.ACTION_SCREEN_ON:
                    onScreenOn();
                    break;
            }
        }
    }
}
